from PIL import Image

def pad_image(input_path, output_path, target_size=(1000, 600), fill_color=(0, 0, 0)):
    """
    将图片填充到指定大小，并居中
    input_path: 输入图片路径
    output_path: 输出图片路径
    target_size: (width, height)
    fill_color: 填充颜色，RGB 元组，例如黑色 (0,0,0) 或白色 (255,255,255)
    """
    img = Image.open(input_path)
    target_width, target_height = target_size

    # 创建新画布
    new_img = Image.new("RGB", target_size, fill_color)

    # 计算原图在新画布中的位置（居中）
    left = (target_width - img.width) // 2
    top = (target_height - img.height) // 2

    # 将原图粘贴到新画布
    new_img.paste(img, (left, top))

    # 保存
    new_img.save(output_path)
    print(f"✅ 输出已保存到 {output_path}")

if __name__ == "__main__":
    input_path = r"C:\Users\33720\Desktop\2.png"
    output_path = r"C:\Users\33720\Desktop\output.jpg"
    pad_image(input_path, output_path, target_size=(1000, 600), fill_color=(255, 255, 255))  # 白色填充
    # pad_image(input_path, output_path, target_size=(1000, 600), fill_color=(0, 0, 0))  # 白色填充
